package com.lanou3g.exam.model;

import com.lanou3g.exam.pojo.User;
import com.lanou3g.exam.util.JDBCUitl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class RegisterModel {
    public boolean register(User user) {
        Connection conn = JDBCUitl.getConn();
        {
            try {
                String sqlInert = "INSERT INTO user(username,pwd,nickname)Values(?,?,?)";
                PreparedStatement psInsert = conn.prepareStatement(sqlInert);
                psInsert.setString(1, user.getUsername());
                psInsert.setString(2, user.getPwd());
                psInsert.setString(3, user.getNickname());
                psInsert.executeUpdate();
                psInsert.close();
                return true;
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return false;
    }
public boolean hasThisName(String username){
        String sql="select count(*) from user where username = ?";
        Connection conn=JDBCUitl.getConn();
    try {
        PreparedStatement ps= conn.prepareStatement(sql);
        ps.setString(1,username);
        ResultSet rs=ps.executeQuery();
        rs.next();
        int count =rs.getInt(1);
        rs.close();
        ps.close();
        return count>0;
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return false;
}
}

